package com.molyfun.parents.im;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.molyfun.parents.utils.ActivityManager;
import com.molyfun.parents.utils.LoginTIMUtil;
import com.tencent.imsdk.TIMUserStatusListener;

/**
 * //实现ConnectionListener接口-----监听网络状态---在另外一台手机登录----账号被移除
 * Created by Administrator on 2016/8/12.
 */
public class MyConnectionListener implements TIMUserStatusListener {
    Activity context;

    public MyConnectionListener(Activity context) {
        this.context = context;
    }

    public void showHint() {
        if (!ActivityManager.getInstance().isForeground(context)) {
            return;
        }
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示")
                        .setMessage("咨询管理服务已断开连接，是否重新连接？")
                        .setCancelable(false)
                        .setNegativeButton("取消", null)
                        .setNeutralButton("重新登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LoginTIMUtil.getInstance().loginTIM(new LoginTIMUtil.ILoginTIMCallback() {
                                    @Override
                                    public void onSuccess() {
                                    }

                                    @Override
                                    public void onError() {
                                        showHint();
                                    }
                                });
                                dialog.dismiss();
                            }
                        }).create().show();
            }
        });
    }

    public void showHint(final LoginHxCallback loginHxCallback) {
        if (!ActivityManager.getInstance().isForeground(context)) {
            return;
        }
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示")
                        .setMessage("咨询管理服务已断开连接，是否重新连接？")
                        .setCancelable(false)
                        .setNegativeButton("取消", null)
                        .setNeutralButton("重新登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LoginTIMUtil.getInstance().loginTIM(new LoginTIMUtil.ILoginTIMCallback() {
                                    @Override
                                    public void onSuccess() {
                                        if (loginHxCallback != null) {
                                            loginHxCallback.onSuccess();
                                        }
                                    }

                                    @Override
                                    public void onError() {
                                        showHint(loginHxCallback);
                                    }
                                });
                                dialog.dismiss();
                            }
                        }).create().show();
            }
        });
    }

    @Override
    public void onForceOffline() {
        showHint();
    }

    @Override
    public void onUserSigExpired() {

    }

    public interface LoginHxCallback {
        void onSuccess();
    }

}